package com.doctor.ui.consulting.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;

import com.doctor.adapter.SubMenuAdapter2;
import com.doctor.comm.FragmentHelper;
import com.doctor.constants.CodeHelper;
import com.doctor.database.DbOperator;
import com.doctor.ui.BaseFragmentActivity;
import com.doctor.ui.R;
import com.doctor.ui.consulting.BookServiceFrag;
import com.doctor.ui.consulting.BookServiceFrag.BookServiceFragCallback;
import com.doctor.ui.consulting.ConsultationMenu.view.ConsultationMenuFrag;
import com.doctor.ui.consulting.TechConsultingFrag;
import com.doctor.ui.consulting.TechConsultingFrag.TechConsultingFragCallback;
import com.doctor.ui.consulting.consultationCardRecord.view.ConsultationCardRecordFrag;
import com.doctor.ui.consulting.professorList.view.ProfessorListFrag;
import com.doctor.ui.homedoctor.HomeDoctorActivity;
import com.doctor.ui.medicalknowledge.KnowledgeActivity;
import com.doctor.ui.office.OfficeActivity;
import com.doctor.utils.callback.DataChangedListener;

import java.io.Serializable;
import java.util.Map;

public class ConsultingActivity extends BaseFragmentActivity implements TechConsultingFragCallback,BookServiceFragCallback, DataChangedListener {

	/**
	 * 远程会诊  双向转诊
	 */
	
	private final String[] items={"技术咨询/技术指导\n技术交流/技术协作","分级诊疗/预约挂号\n预约住院/预约治疗"};
	private SubMenuAdapter2 subMenuAdapter2;
	private TechConsultingFrag techConsultingFrag;
	private BookServiceFrag bookServiceFrag;
	//id和科目map
	public static Map<Integer, String> map1;
	//科目和id map
	public static Map<String, Integer> map2;
	private FrameLayout mConsultationContainer;
	private boolean isPrefessor;//是否是专家的标志


	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.base_activity2cnew);
		
		initiaView();
		//*****************以下两项3.11删除,由于新需求,新的界面*********************//
//		initiaSubMenu();
//		getData();
		//*****************以上两项3.11删除,由于新需求,新的界面********************//

//		//通知用户升级新数据
		noticeNewData();
		//// TODO: 2017/3/12 下面开始新页面的开发
		initData();
	}

	private void initData() {
		//判断是普通会员还是专家
		if (!isPrefessor){
			//开始填充普通会员的界面
			ConsultationListFrag consultationListFrag = new ConsultationListFrag();
			consultationListFrag.setReplaceFragmentListener(this);
			FragmentManager fragmentManager=getSupportFragmentManager();
			FragmentTransaction mFragmentTransaction = fragmentManager.beginTransaction();
			mFragmentTransaction.replace(R.id.fl_consultation_container, consultationListFrag).commit();
		}else {
			//开始填充专家的界面

		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		//改变主菜单背景颜色
		officeBtn.setBackgroundResource(R.color.light_green);
		knowledgeBtn.setBackgroundResource(R.color.light_green);
		consultingBtn.setBackgroundResource(R.color.green);
		homeDocBtn.setBackgroundResource(R.color.light_green);
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		//改变主菜单背景颜色
		consultingBtn.setBackgroundResource(R.color.light_green);
	}

	@Override
	protected void initiaView() {
		// TODO Auto-generated method stub
		super.initiaView();
		//隐藏搜索
		searchText.setVisibility(View.GONE);
		searchBtn.setVisibility(View.GONE);
		search.setVisibility(View.INVISIBLE);
		//下面开始新的开发===================================================================
		mConsultationContainer = (FrameLayout) findViewById(R.id.fl_consultation_container);
	}

	//--------------------界面切换的listener事件处理------------
	@Override
	public void dataChanged(int flag, Object obj) {
		replaceFragment(flag,obj);
	}

	/**
	 * 根据标记替换fragment的方法
	 * @param flag
	 */
	private void replaceFragment(int flag,Object object) {
		FragmentManager fragmentManager=getSupportFragmentManager();
		FragmentTransaction mFragmentTransaction = fragmentManager.beginTransaction();
		switch (flag){
			case CodeHelper.CONSULTATION_MENU://切换到会诊列表页面
				ConsultationMenuFrag consultationMenuFrag = new ConsultationMenuFrag();
				consultationMenuFrag.setDatachangListener(this);
				mFragmentTransaction.replace(R.id.fl_consultation_container, consultationMenuFrag).commit();
				break;
			case CodeHelper.PROFESSOR_LIST_FRAGMENT://切换到专家列表页面
				ProfessorListFrag professorListFrag = new ProfessorListFrag();
				professorListFrag.setDatachangListener(this);
				if (object!=null){
					Bundle bundle=new Bundle();
					bundle.putSerializable("sickenInfoEntity", (Serializable) object);
					professorListFrag.setArguments(bundle);
				}
				mFragmentTransaction.replace(R.id.fl_consultation_container, professorListFrag).commit();
				break;
			case CodeHelper.CONSULTATION_CARD_RECORD_FRAGMENT://切换到会诊单记录界面
				ConsultationCardRecordFrag consultationListRecordFrag=new ConsultationCardRecordFrag();
				consultationListRecordFrag.setDatachangListener(this);
				mFragmentTransaction.replace(R.id.fl_consultation_container,consultationListRecordFrag).commit();
				break;

			default:
				break;
		}
	}



	@Override
	protected void initiaSubMenu() {
		// TODO Auto-generated method stub
		super.initiaSubMenu();

		map1=DbOperator.getInstance(ConsultingActivity.this).selectAdvantage1();
		map2=DbOperator.getInstance(ConsultingActivity.this).selectAdvantage2();

		FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
		if (techConsultingFrag==null) {
			techConsultingFrag=new TechConsultingFrag();
		}
		transaction.replace(fragContainer.getId(), techConsultingFrag).commit();

		subMenuAdapter2=new SubMenuAdapter2(ConsultingActivity.this, items);
		subMenu.setAdapter(subMenuAdapter2);
		subMenu.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
				switch (arg2) {
				case 0:
					if (subMenuAdapter2.getSelectedPosition()!=0) {
						if (techConsultingFrag==null) {
							techConsultingFrag=new TechConsultingFrag();
						}
						FragmentHelper.setFragSwitchAnimation(transaction);
						transaction.replace(fragContainer.getId(), techConsultingFrag);
						transaction.addToBackStack(null);
						transaction.commit();
						subMenuAdapter2.setSelectedPosition(arg2);
					}
					break;

				case 1:
					if (subMenuAdapter2.getSelectedPosition()!=1) {
						if (bookServiceFrag==null) {
							bookServiceFrag=new BookServiceFrag();
						}
						FragmentHelper.setFragSwitchAnimation(transaction);
						transaction.replace(fragContainer.getId(), bookServiceFrag);
						transaction.addToBackStack(null);
						transaction.commit();
						subMenuAdapter2.setSelectedPosition(arg2);
					}
					break;
				}
			}
		});
	}

	@Override
	protected void onHeaderClicked() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onOfficeClicked() {
		// TODO Auto-generated method stub
		final Intent intent=new Intent(ConsultingActivity.this,OfficeActivity.class);
		startActivity(intent);
		finish();
	}

	@Override
	protected void onMedicalKnowledgeClicked() {
		// TODO Auto-generated method stub
		final Intent intent=new Intent(ConsultingActivity.this,KnowledgeActivity.class);
		startActivity(intent);
		finish();
	}



	@Override
	protected void onHomeDoctorClicked() {
		// TODO Auto-generated method stub
		final Intent intent=new Intent(ConsultingActivity.this, HomeDoctorActivity.class);
		startActivity(intent);
		finish();
	}

	@Override
	public void onTechConsulting() {
		// TODO Auto-generated method stub
		subMenuAdapter2.setSelectedPosition(0);
	}


	@Override
	public void onBookService() {
		// TODO Auto-generated method stub
		subMenuAdapter2.setSelectedPosition(1);
	}
}
